package com.example.smartcity_40.Outpatient;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity_40.Outpatient.bean.StatusBean;
import com.example.smartcity_40.R;
import com.example.smartcity_40.tool.Tool;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class AddReservationActivity extends AppCompatActivity {

    private AddReservationActivity context;
    private String TYPE = "";
    private Tool tool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_reservation);
        context = this;
        initview();
    }

    private void initview() {
        ImageView back = findViewById(R.id.back);

        EditText ed_name = findViewById(R.id.ed_name);
        TextView ed_birthday = findViewById(R.id.ed_birthday);
        RadioGroup sex_option = findViewById(R.id.sex_option);
        TextView save = findViewById(R.id.save);
        JSONObject jsonObject = new JSONObject();
        tool = new Tool(context);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });

        sex_option.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int checkedRadioButtonId = group.getCheckedRadioButtonId();
                if (R.id.sex_male == checkedRadioButtonId) {
                    TYPE = "1";
                } else if (R.id.sex_female == checkedRadioButtonId) {
                    TYPE = "2";
                }
            }
        });

        ed_birthday.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = new Date(year, month + 1, dayOfMonth);
                        String format = simpleDateFormat.format(date);
                        ed_birthday.setText(format);
                    }
                }, 2022, 11, 6);
                datePickerDialog.show();
            }

        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String name = ed_name.getText().toString();
                  //  String birthday = ed_birthday.getText().toString();

                    if (!TextUtils.isEmpty(name)
                            && !TextUtils.isEmpty(TYPE)
                    ) {

                        jsonObject.put("patientName", name);
                        jsonObject.put("categoryId", 3);
                        //jsonObject.put("birthday", date);
                        jsonObject.put("money", 6);
                        jsonObject.put("type", TYPE);

                        addvisitor(jsonObject.toString());
                    } else {
                        Toast.makeText(context, "填写的信息不能为空", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void addvisitor(String JSON) {
        tool.send("/prod-api/api/hospital",
                Tool.POST,
                JSON,
                true,
                StatusBean.class,
                new Function1<StatusBean, Unit>() {
                    @Override
                    public Unit invoke(StatusBean statusBean) {
                        if (statusBean.getCode() == 200) {
                            Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            setResult(RESULT_OK,intent);
                            context.finish();
                        } else {
                            Toast.makeText(context, statusBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                        return null;
                    }
                });

    }
}